package com.xhwl.centiotdata.suppliersystem.mili;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.MiLiConstant;
import com.xhwl.centiotdata.pojo.dto.mili.FrameMiLiDTO;
import com.xhwl.centiotdata.pojo.dto.mili.LoginMiLiDTO;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.common.enums.OrganizationTypeEnum;
import com.xhwl.common.pojo.cent.sdata.Organization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: wuyuhao
 * @createDate: 2022/10/19 15:43
 */
@Service
@Slf4j
public class FrameMiLiHandler {

    @Autowired
    private GeneralMiLiHandler generalMiLiHandler;

    public boolean addFrame(int type, PedestrianIntegrationConfigDTO config, String frameNo) {
        String url = null;
        if (OrganizationTypeEnum.BUILDING.getId().intValue() == type) {
            url = MiLiConstant.FRAME_INFO_BUILDING_ADD_URL;
        } else if (OrganizationTypeEnum.UNIT.getId().intValue() == type) {
            url = MiLiConstant.FRAME_INFO_UNIT_ADD_URL;
        } else if (OrganizationTypeEnum.ROOM.getId().intValue() == type) {
            url = MiLiConstant.FRAME_INFO_HOUSE_ADD_URL;
        }
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(config.getLoginName(),
                config.getPassword(), config.getIpAndPort());

        JSONObject object = new JSONObject();
        object.put("FrameNo", frameNo);
        String response = generalMiLiHandler.doBusinessRequest(config.getLoginName(), loginInfo,
                config.getIpAndPort(), url, object);
        log.info("addFrame->response: {}", response);
        // 解析返回的设备列表数据
        JSONObject res = JSON.parseObject(response);
        int result = res.getInteger("Result");
        return result == 0;
    }

    public boolean delFrame(PedestrianIntegrationConfigDTO config, String frameNo) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(config.getLoginName(),
                config.getPassword(), config.getIpAndPort());

        JSONObject object = new JSONObject();
        object.put("FrameNo", frameNo);
        String response = generalMiLiHandler.doBusinessRequest(config.getLoginName(), loginInfo,
                config.getIpAndPort(), MiLiConstant.FRAME_INFO_FRAME_DELETE_URL, object);
        log.info("delFrame->response: {}", response);
        // 解析返回的设备列表数据
        JSONObject res = JSON.parseObject(response);
        int result = res.getInteger("Result");
        return result == 0;
    }

    public boolean editFrame(Organization org, PedestrianIntegrationConfigDTO config, String frameNo) {
        String url = null;
        if (OrganizationTypeEnum.BUILDING.getId().intValue() == org.getType()) {
            url = MiLiConstant.FRAME_INFO_BUILDING_UPDATE_URL;
        } else if (OrganizationTypeEnum.UNIT.getId().intValue() == org.getType()) {
            url = MiLiConstant.FRAME_INFO_UNIT_UPDATE_URL;
        } else if (OrganizationTypeEnum.ROOM.getId().intValue() == org.getType()) {
            url = MiLiConstant.FRAME_INFO_HOUSE_UPDATE_URL;
        }
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(config.getLoginName(),
                config.getPassword(), config.getIpAndPort());

        JSONObject object = new JSONObject();
        object.put("FrameNo", frameNo);
        object.put("Name", org.getName());
        String response = generalMiLiHandler.doBusinessRequest(config.getLoginName(), loginInfo,
                config.getIpAndPort(), url, object);
        log.info("editFrame->response: {}", response);
        // 解析返回的设备列表数据
        JSONObject res = JSON.parseObject(response);
        int result = res.getInteger("Result");
        return result == 0;
    }

    /**
     * ParentFrameNo 父框架 ID
     * Cascaded 级联的
     * MaxLevel 最大层级数
     *
     * @param object
     * @param config
     * @return
     */
    public List<FrameMiLiDTO> queryFrame(PedestrianIntegrationConfigDTO config, JSONObject object) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(config.getLoginName(),
                config.getPassword(), config.getIpAndPort());

        String response = generalMiLiHandler.doBusinessRequest(config.getLoginName(), loginInfo,
                config.getIpAndPort(), MiLiConstant.FRAME_QUERY_URL, object);
        // 解析返回的设备列表数据
        JSONObject res = JSON.parseObject(response);
        JSONArray body = res.getJSONArray("Body");
        return body.toJavaList(FrameMiLiDTO.class);
    }

}
